package com.jingtao.product.controller;

import com.jingtao.common.core.web.controller.BaseController;
import com.jingtao.common.core.web.domain.AjaxResult;
import com.jingtao.common.core.web.page.TableDataInfo;
import com.jingtao.common.log.annotation.Log;
import com.jingtao.common.log.enums.BusinessType;
import com.jingtao.product.domain.DTO.ProductAttributeCategoryItem;
import com.jingtao.product.domain.ProductAttributeCategory;
import com.jingtao.product.domain.Result.CommonPage;
import com.jingtao.product.domain.Result.CommonResult;
import com.jingtao.product.service.IProductAttributeCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品属性分类Controller
 *
 * @author jingtao
 * @date 2024-04-05
 */
@RestController
@Api(tags = "商品属性分类管理")
@RequestMapping("/productAttribute/category")
public class ProductAttributeCategoryController extends BaseController
{
    @Autowired
    private IProductAttributeCategoryService productAttributeCategoryService;

    @ApiOperation("分页获取所有商品属性分类")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProductAttributeCategory>> getList(@RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
        List<ProductAttributeCategory> productAttributeCategoryList = productAttributeCategoryService.getList(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(productAttributeCategoryList));
    }

    @ApiOperation("添加商品属性分类")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestParam String name) {
        int count = productAttributeCategoryService.create(name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品属性分类")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestParam String name) {
        int count = productAttributeCategoryService.update(id, name);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除单个商品属性分类")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = productAttributeCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("获取单个商品属性分类信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProductAttributeCategory> getItem(@PathVariable Long id) {
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getItem(id);
        return CommonResult.success(productAttributeCategory);
    }


    @ApiOperation("获取所有商品属性分类及其下属性")
    @RequestMapping(value = "/list/withAttr", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ProductAttributeCategoryItem>> getListWithAttr() {
        List<ProductAttributeCategoryItem> productAttributeCategoryResultList = productAttributeCategoryService.getListWithAttr();
        return CommonResult.success(productAttributeCategoryResultList);
    }

    /**
     * 查询产品属性分类列表
     */
    @GetMapping("/r/list")
    public TableDataInfo list(ProductAttributeCategory productAttributeCategory)
    {
        startPage();
        List<ProductAttributeCategory> list = productAttributeCategoryService.selectProductAttributeCategoryList(productAttributeCategory);
        return getDataTable(list);
    }


    /**
     * 获取产品属性分类详细信息
     */
    @GetMapping(value = "/r/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productAttributeCategoryService.selectProductAttributeCategoryById(id));
    }

    /**
     * 新增产品属性分类
     */
    @Log(title = "产品属性分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductAttributeCategory productAttributeCategory)
    {
        return toAjax(productAttributeCategoryService.insertProductAttributeCategory(productAttributeCategory));
    }

    /**
     * 修改产品属性分类
     */
    @Log(title = "产品属性分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductAttributeCategory productAttributeCategory)
    {
        return toAjax(productAttributeCategoryService.updateProductAttributeCategory(productAttributeCategory));
    }

    /**
     * 删除产品属性分类
     */
    @Log(title = "产品属性分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productAttributeCategoryService.deleteProductAttributeCategoryByIds(ids));
    }
}
